#include "../StdInc.h"

#ifdef BUILD_LTS

#include "LtsDataBaseHelper.h"
#include "DataBase.h"
#include "../Primitive/lts/LTSTradeApi.h"
#include "../Primitive/lts/LTSMarketDataApi.h"

namespace GSTrader
{
	namespace TradeService
	{
		CLtsDataBaseHelper::CLtsDataBaseHelper(CDataBase* pDataBase) : CDatabaseHelperBase(pDataBase)
		{
		}

		CLtsDataBaseHelper::~CLtsDataBaseHelper()
		{
		}

		CMsg* CLtsDataBaseHelper::UpdateErrorMsg(CDataApi* pTradeApi, CSecurityFtdcRspInfoField* pRspInfo)
		{
			CDatabaseAttach dbAttach(m_pDataBase);

			if (pRspInfo && pRspInfo->ErrorID != 0)
			{
				MakeDataBaseTradeMsg(rspError, CErrorMsg, CErrorMsgTable);
				pTheData->m_nErrorID	= pRspInfo->ErrorID;
				pTheData->m_nProviderId	= pTradeApi->m_nProviderId;
				pTheData->m_strErrorMsg	= pRspInfo->ErrorMsg;
				pTheData->m_nApiType	= pTradeApi->m_nApiType;
				pTheData->m_nType		= pTradeApi->m_nPlatformType;
				insert(*pDataDef);
				return pMsg;
			}

			return NULL;
		}

		CMsg* CLtsDataBaseHelper::UpdateMarkDataLoginInfo(CLtsMarketDataApi* pMarketDataApi, CSecurityFtdcRspUserLoginField* pField)
		{
			if (pField != NULL)
			{
				string strInvestorID = pMarketDataApi->m_pConnectionInfo->m_strInvestorID;
				string strBrokerID = pMarketDataApi->m_pConnectionInfo->GetBrokerID();

				MakeDataBaseMarketDataMsg(msgMarketDataConnected, CAccountData, CAccountDataTable);

				pTheData->m_nClientType = ptLts;
				pTheData->m_nProviderId	= pMarketDataApi->m_nProviderId;
				pTheData->m_tradingDay	= pField->TradingDay;
				pTheData->m_strBrokerID = strBrokerID;
				pTheData->m_strInvestorID = strInvestorID;
				pTheData->m_strUserID	= strInvestorID;
				pTheData->m_nApiData		= (int)pField;
				pTheData->m_nApiType		= pMarketDataApi->m_nApiType;
				pTheData->m_pConnectionInfo = pMarketDataApi->m_pConnectionInfo;

				if (UpdateLoginInfo(pMarketDataApi, pDataDef))
				{
					return pMsg;
				}
				else
				{
					delete pMsg;
				}
			}

			return NULL;
		}

		CMsg* CLtsDataBaseHelper::UpdateTradeLoginInfo(CLtsTradeApi* pTradeApi, CSecurityFtdcRspUserLoginField* pField)
		{
			if (pField != NULL)
			{
				MakeDataBaseTradeMsg(rspTradeLogin, CAccountData, CAccountDataTable);

				pTheData->m_nClientType		= ptLts;
				pTheData->m_nProviderId		= pTradeApi->m_nProviderId;
				pTheData->m_tradingDay		= pField->TradingDay;
				pTheData->m_strBrokerID		= pField->BrokerID;
				pTheData->m_strInvestorID	= pField->UserID;
				pTheData->m_strUserID		= pField->UserID;
				pTheData->m_nApiData		= (int)pTradeApi;
				pTheData->m_nApiType		= pTradeApi->m_nApiType;
				pTheData->m_strLogintime	= pField->LoginTime;
				pTheData->m_pConnectionInfo = pTradeApi->m_pConnectionInfo;

				if (UpdateLoginInfo(pTradeApi, pDataDef))
				{
					return pMsg;
				}
				else
				{
					delete pMsg;
				}
			}
			return NULL;
		}

		CMsg* CLtsDataBaseHelper::UpdateInstrument(CLtsTradeApi* pTradeApi, CSecurityFtdcInstrumentField* pInstrument, bool bAttach, bool bDetach)
		{
			if (pInstrument)
			{
				MakeDataBaseTradeMsg(rspInstrument, CAccountInstrument, CAccountInstrumentTable);
				CInstrument ins;
				m_ltsConvert.ConvertInstrument(pInstrument, &ins);

				if (UpdateInstrumentData(pTradeApi, pDataDef, &ins, bAttach, bDetach))
				{
					return pMsg;
				}
				else
				{
					delete pMsg;
				}
			}

			return NULL;
		}

		CMsg* CLtsDataBaseHelper::UpdatePosition(CLtsTradeApi* pTradeApi, CSecurityFtdcInvestorPositionField* pInvestorPosition, bool bAttach, bool bDetach)
		{
			if (pInvestorPosition)
			{
				MakeDataBaseTradeMsg(rspPosition, CPosition, CPositionTable);

				pTheData->m_nProviderId = pTradeApi->m_nProviderId;
				m_ltsConvert.ConvertPosition(pInvestorPosition, pTheData);

				if (UpdatePositionData(pTradeApi, pDataDef, bAttach, bDetach, false))
				{
					return pMsg;
				}
				else
				{
					delete pMsg;
				}
			}
			return NULL;
		}

		CMsg* CLtsDataBaseHelper::UpdateOrder(CLtsTradeApi* pTradeApi, CSecurityFtdcOrderField* pOrder, bool bAttach, bool bDetach)
		{
			if (pOrder && strlen(pOrder->InstrumentID) > 0)
			{
				MakeDataBaseTradeMsg(rspOrder, COrder, COrderTable);
				pTheData->m_nProviderId = pTradeApi->m_nProviderId;
				m_ltsConvert.ConvertOrder(pOrder, pTheData);

				if (UpdateOrderData(pTradeApi, pDataDef, bAttach, bDetach))
				{
					return pMsg;
				}
				else
				{
					delete pMsg;
				}
			}

			return NULL;
		}

		CMsg* CLtsDataBaseHelper::UpdateTrade(CLtsTradeApi* pTradeApi, CSecurityFtdcTradeField* pTrade, bool bAttach, bool bDetach)
		{
			if (pTrade)
			{
				MakeDataBaseTradeMsg(rspTrade, CTrade, CTradeTable);

				pTheData->m_nProviderId = pTradeApi->m_nProviderId;
				m_ltsConvert.ConvertTrade(pTrade, pTheData);

				if (UpdateTradeData(pTradeApi, pDataDef, bAttach, bDetach))
				{
					return pMsg;
				}
				else
				{
					delete pMsg;
				}
			}

			return NULL;
		}

		CMsg* CLtsDataBaseHelper::UpdateTradingAccount(CLtsTradeApi* pTradeApi, CSecurityFtdcTradingAccountField* pTradingAccount)
		{
			CDatabaseAttach dbAttach(m_pDataBase);
			if (pTradingAccount && strlen(pTradingAccount->AccountID) > 0)
			{
				MakeDataBaseTradeMsg(rspTradingAccount, CTradingAccount, CTradingAccountTable);

				pTheData->m_nProviderId = pTradeApi->m_nProviderId;
				m_ltsConvert.ConvertTradingAccount(pTradingAccount, pTheData);

				if (UpdateTradingAccountData(pTradeApi, pDataDef))
				{
					return pMsg;
				}
				else
				{
					delete pMsg;
				}
			}

			return NULL;
		}

		CMsg* CLtsDataBaseHelper::UpdateMarketData(CMarketDataApi* pMarketDataApi, CSecurityFtdcDepthMarketDataField* pDepthMarketData)
		{
			CDatabaseAttach attach(m_pDataBase);

			if (pMarketDataApi == NULL || pDepthMarketData == NULL)
				return NULL;

			MakeObjectMsg(msgOnNewQuoteData, CDepthMarketData);

			m_ltsConvert.ConvertMarketData(pDepthMarketData, pObject);
			pObject->m_nLastUpdateProviderId = pMarketDataApi->m_nProviderId;

			if (UpdateDepthMarketData(pMarketDataApi, pObject))
			{
				return pMsg;
			}
			else
			{
				delete pMsg;
				return nullptr;
			}
		}

		bool CLtsDataBaseHelper::GetOrder(CTradeApi* pTradeApi, COrderTable* input, OrderCursor& output)
		{
			assert(false);
			return false;
		}

		bool CLtsDataBaseHelper::GetInputOrder(OrderCursor& orderCursor, InputOrderCursor& output)
		{
			dbQuery dbQry;
			dbQry = "m_nSessionId = ", orderCursor->m_nSessionId, " and m_nFrontId = ",
			orderCursor->m_nFrontId, " and m_strOrigOrdId =", orderCursor->m_strOrigOrdId;

			if (output.select(dbQry) > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}

#endif